package com.yunxun.coinmessage.controller;

import com.yunxun.coinmessage.entity.Coin;
import com.yunxun.coinmessage.entity.Exchange;
import com.yunxun.coinmessage.entity.MesSource;
import com.yunxun.coinmessage.templatedata.MsgResult;
import com.yunxun.coinmessage.templatedata.mesSorurceTemplateData;
import com.yunxun.coinmessage.service.CoinService;
import com.yunxun.coinmessage.service.ExchangeService;
import com.yunxun.coinmessage.service.MesSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class MesSourceController {
    @Autowired
    MesSourceService mesResourceService;
     @Autowired//为了实现同步删除
    CoinService coinService;
     @Autowired
    ExchangeService exchangeService;//为了实现同步删除
    @RequestMapping("/addMesSource")
    public String addMesSource(MesSource mesSource , Model model){

        //判断类型选择要弹出的窗口
        if(mesSource.getTypeId()==1) {
            model.addAttribute("mesSource",mesSource);
            return "coin-add";
        }
        if(mesSource.getTypeId()==2){
            model.addAttribute("mesSource",mesSource);
            return "exchange-add";
        }
        if(mesSource.getTypeId()==3){
            model.addAttribute("mesSource",mesSource);
            return "null";
        }
        return null;
    }

    @RequestMapping("/editCoinOrExchange")
    public String edit(MesSource mesSource , Model model){

        //判断类型选择要弹出的窗口
        if(mesSource.getTypeId()==1) {
            model.addAttribute("mesSource",mesSource);
            Coin coin= coinService.queryById(mesSource.getSid());
            model.addAttribute("coin",coin);
            model.addAttribute("flag",false);//该标在exchange-edit中判断是仅更新exchange还是同时更新渠道
            return "coin-edit";


        }
        if(mesSource.getTypeId()==2){
            model.addAttribute("mesSource",mesSource);
            Exchange exchange=exchangeService.queryById(mesSource.getSid());
            model.addAttribute("exchange",exchange);
            model.addAttribute("flag",false);//该标在exchange-edit中判断是仅更新exchange还是同时更新渠道
            return "exchange-edit";
        }
        if(mesSource.getTypeId()==3){
            model.addAttribute("mesSource",mesSource);
            return "null";
        }
        return null;
    }



    @RequestMapping("/querymesSourceAll")
    public String queryAll(Model model){
       List<mesSorurceTemplateData> mesSorurceTemplateDataList =mesResourceService.queryAll();
       Integer total=mesResourceService.queryTotal();
       model.addAttribute("total",total);
       model.addAttribute("mesSourceModelList", mesSorurceTemplateDataList);
       return "mesSource-list";
    }

    @RequestMapping("/queryMesSourceByName")
    @ResponseBody
    public MsgResult queryByName(String sname){
        MsgResult msgResult=new MsgResult();
        if(mesResourceService.queryByName(sname)!=null){
            msgResult.setStatus(true);
            msgResult.setMsg("该渠道已存在");
        }else{
            msgResult.setStatus(false);
            msgResult.setMsg("");
        }
        return msgResult;
}

}
